package com.flower8.service.impl;

import com.flower8.dao.CreditPointDao;
import com.flower8.dao.MessageDao;
import com.flower8.dto.UserCreditPointDto;
import com.flower8.model.UserAppeal;
import com.flower8.model.UserCreditPoint;
import com.flower8.service.CreditPointService;
import com.flower8.service.CrowdfundService;
import com.flower8.sysconfig.constants.SysConstants;
import com.flower8.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 信用分service实现层
 * @Author: tanglin
 * @Date: 2019/12/23 16:02
 * @Version: 1.0
 */
@Service
public class CreditPointServiceImpl implements CreditPointService {
    @Autowired
    private CreditPointDao creditPointDao;
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private CrowdfundService crowdfundService;

    /**
     * 查询信用分申诉列表
     * @param pd
     * @return
     */
    @Override
    public List<UserCreditPointDto> getAppealList(PageData pd) {
        return creditPointDao.getAppealList(pd);
    }

    /**
     * 查询信用分申诉列表数量
     * @param pd
     * @return
     */
    @Override
    public int getAppealListCount(PageData pd) {
        return creditPointDao.getAppealListCount(pd);
    }

    /**
     * 申诉处理
     * @param pd
     */
    @Override
    @Transactional
    public void updateAppeal(PageData pd)throws Exception {
        UserAppeal userAppeal = creditPointDao.getAppealById(pd);
        long uid = userAppeal.getUid();
        long crowdfunId = userAppeal.getBusinessId();
        //处理申诉单
        creditPointDao.updateAppleal(pd);
        //成功的申诉，恢复信用分
        if("1".equals(pd.getString("flag"))){
            UserCreditPoint userCreditPoint = creditPointDao.getCreditPointByCrowd(crowdfunId);
            Map creditMap = new HashMap();
            creditMap.put("uid",uid);
            creditMap.put("crowdfunId",crowdfunId);
            creditMap.put("creditPoint",-userCreditPoint.getCreditPoint());
            creditMap.put("type",6);
            creditMap.put("reason","诚信值申诉成功");

            creditPointDao.addCreditPoint(creditMap);

            //更新用户总信用分
            Map totalCreditMap = new HashMap();
            totalCreditMap.put("creditPoint",-userCreditPoint.getCreditPoint());
            totalCreditMap.put("uid",uid);
            creditPointDao.updateTotalCreditPoint(totalCreditMap);

            PageData pageDate = new PageData();
            pageDate.put("cid", crowdfunId);
            pageDate.put("status", 1);
            crowdfundService.updCrowd(pageDate);
        }

        //发送一条消息通知
        Map message = new HashMap();
        message.put("uid",uid);
        message.put("type",1);
        message.put("category",6);
        message.put("icon", SysConstants.MSG1);
        if("1".equals(pd.getString("flag"))){
            message.put("contents","诚信值申诉成功");
        }else{
            message.put("contents","诚信值申诉失败");
        }

        messageDao.addMsg(message);
    }
}
